<?php
namespace app\index\controller;

use app\common\controller\BaseHome;
use app\common\model\Guestbook;

class Index extends BaseHome {
    public function _initialize(){
        parent::_initialize();
    }

    public function index() {
        return $this->fetch();
    }

    public function newarc($page){
        $archive = new \app\common\model\Archive();
        $where['status'] = 1;
        $dataList = $archive->where($where)->order('id desc')->page($page.', 5')->select();
        foreach ($dataList as $k => $val){
        	if( empty($val['litpic']) ){
            	$imgarr = getImgs($val['addonarticle']['content']);
            	$val['litpic'] = $imgarr[0];
            }
            $flag_arr = explode(',', $val['flag']);
            if(in_array('j',$flag_arr) && !empty($val['jumplink'])){
                $dataList[$k]['arcurl'] = $val['jumplink'];
            }else{
                $dataList[$k]['arcurl'] = url('detail/'.$val->arctype->dirs.'/'.$val['id']);
            }
        }
        $this->assign('dataList', $dataList);
        return $this->fetch('inc/new_arc');
    }

    public function create() {	//提交留言
        if (request()->isPost()){
    		$GuestbookModel = new Guestbook();
            $data = input('post.');
			if(empty($this->uid)){
				return ajaxReturn('请登录留言','',2);
			}
            $result = $GuestbookModel->validate('Guestbook.add')->allowField(true)->save($data);
            if ($result){
                return ajaxReturn('留言成功', url('index'));
            }else{
                return ajaxReturn($GuestbookModel->getError());
            }
        }else{
            return $this->fetch();
        }
    }

	public function abc(){
		$arctype = new \app\common\model\Arctype;
		$archive = new \app\common\model\Archive;
		$zanlog = new \app\common\model\ZanLog;
		$arc_reply = new \app\common\model\ArchiveReply;

		$ah_data = $arc_reply->newReplyList();

		// 从文件中读取数据到PHP变量
//		$json_string = file_get_contents('abc.json');
		// 把JSON字符串转成PHP数组
//		$data = json_decode($json_string, true);
//		p($data);

		// 生成一个PHP数组
		$data = array();
		$data[0] = array('1','吴者然','onestopweb.cn');
		$data[1] = array('2','何开','iteye.com');
		// 把PHP数组转成JSON字符串
		$json_string = json_encode($data);
//		 写入文件
//		file_put_contents('abc.json', $json_string);

//		unlink(WEB_PATH.'/uploads/image/20171217/20171217185028_880468.jpg');
//		echo WEB_PATH.'/uploads/image/20171217/20171217185028_880468.jpg';
//		foreach ($ah_data as $k => $v) {
//			echo $v['arcurl']. '-----' . $v['create_time'] . '<br />';
//		}
//		p($ah_data[1]['create_time']);

		return $this->fetch();
//		echo APP_PATH;

	}

	public function sui(){

		return $this->fetch();
	}

}
